package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.dao.other.OtherFundChannelConfigMapper;
import com.zjdiepu.www.ibiz.other.IOtherFundChannelConfigService;
import com.zjdiepu.www.model.other.dto.OtherFundChannelConfig;
import com.zjdiepu.www.model.other.ext.OtherFundChannelConfigExt;

/**
 * 第三方资金渠道配置Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年08月03日 18时03分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherFundChannelConfigServiceImpl extends BaseService implements IOtherFundChannelConfigService {
	public static final Logger logger = LoggerFactory.getLogger(OtherFundChannelConfigServiceImpl.class);

	@Autowired
	private OtherFundChannelConfigMapper otherFundChannelConfigMapper;
	
	/**
	 * 根据业务单位编码查找其它渠道资金配置
	 * @param busiUnitCode 业务单位编码(必填)
	 * @param codeFundChannel 资金渠道编码 (选填)
	 * @return
	 */
	@Override
	public List<OtherFundChannelConfig> getOtherFundChannelConfigByUnitCode(String busiUnitCode, String codeFundChannel) {
		List<OtherFundChannelConfig> results = CollectionInitUtil.emptyList();
		if(StringUtils.isBlank(busiUnitCode)) {
			return results;
		}
		
		int busiUnitCodeLength = busiUnitCode.length();
		if(busiUnitCodeLength < 2) {
			return results;
		}
		results = otherFundChannelConfigMapper.selectByUnitCode(busiUnitCode, codeFundChannel);
		if(CollectionUtils.isEmpty(results)) {
			if(busiUnitCodeLength < 4) {
				return results;
			} else {
				String finalUnitCode = null;
				if(busiUnitCodeLength == 4) { //4
					finalUnitCode = busiUnitCode.substring(0, busiUnitCodeLength - 2);
				} else { // > 4
					finalUnitCode = busiUnitCode.substring(0, busiUnitCodeLength - 3);
				}
				return getOtherFundChannelConfigByUnitCode(finalUnitCode, codeFundChannel);
			}
		} else {
			return results;
		}
	}
	
	@Override
	public void saveUpdateAndDelete(OtherFundChannelConfig otherFundChannelConfig, OperateType operType) {
		switch (operType) {
		case add:
			otherFundChannelConfig.initInsertData(otherFundChannelConfig.getModifier(), otherFundChannelConfig.getModifierName());
			otherFundChannelConfigMapper.insert(otherFundChannelConfig);
			break;
		case edit:
			otherFundChannelConfig.initUpdateData(otherFundChannelConfig.getModifier(), otherFundChannelConfig.getModifierName());
			// 影响的记录数
			int updateCount = otherFundChannelConfigMapper.updateByPrimaryKeySelective(otherFundChannelConfig);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherFundChannelConfig.initDeleteData(otherFundChannelConfig.getModifier(), otherFundChannelConfig.getModifierName());
			// 影响的记录数
			int deleteCount = otherFundChannelConfigMapper.updateByPrimaryKeySelective(otherFundChannelConfig);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherFundChannelConfig> selectByExt(OtherFundChannelConfigExt otherFundChannelConfigExt) {
		return otherFundChannelConfigMapper.selectByExt(otherFundChannelConfigExt);
	}
	
	@Override
	public int countByExt(OtherFundChannelConfigExt otherFundChannelConfigExt){
		return otherFundChannelConfigMapper.countByExt(otherFundChannelConfigExt);
	}
	
	@Override
	public OtherFundChannelConfig selectById(String id) {
		return otherFundChannelConfigMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherFundChannelConfigMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherFundChannelConfig otherFundChannelConfig) {
		return otherFundChannelConfigMapper.countRefined(otherFundChannelConfig);
	}

	@Override
	public OtherFundChannelConfig selectRefined(OtherFundChannelConfig otherFundChannelConfig) {
		return otherFundChannelConfigMapper.selectRefined(otherFundChannelConfig);
	}
	
	@Override
	public int deletedRefined(OtherFundChannelConfig otherFundChannelConfig) {
		return otherFundChannelConfigMapper.deletedRefined(otherFundChannelConfig);
	}

	@Override
	public boolean repeat(OtherFundChannelConfig otherFundChannelConfig) {
		int count = countRefined(otherFundChannelConfig);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherFundChannelConfig.getId())) {
			return true;
		}
		OtherFundChannelConfig dbItem = selectRefined(otherFundChannelConfig);
		if(null == dbItem) {
			return false;
		}
		return !otherFundChannelConfig.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherFundChannelConfigMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherFundChannelConfig> otherFundChannelConfigs) {
		return otherFundChannelConfigMapper.insertBatch(otherFundChannelConfigs);
	}
}
